package com.ch.base.taglib;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.log4j.Logger;

import com.ch.base.util.StringUtils;

public class PaginationHeadTag extends TagSupport{
	private final Logger log = Logger.getLogger(this.getClass());
	private String id;
	private String paginationType;
	private String returnChkId;
	private String preActionUrl;
	private String actionUrl;

	public int doStartTag() throws JspException{
		JspWriter out = pageContext.getOut();
		try{
			if(StringUtils.isNotEmpty(returnChkId)){
				out.println("<script>");
				out.println("	var " + returnChkId + " = new ArrayList();");
				out.println("</script>");
			}
			out.println("<input type=\"hidden\" id=\"qryCondition_" + id + "\" name=\"qryCondition\" value=\"\" />");
			out.println("<div id=\"" + id + "\" style=\"display: none;\">");

		}catch(Exception ex){
			throw new JspException(ex);
		}
		return EVAL_BODY_INCLUDE;
	}

	public int doEndTag() throws JspException {
		JspWriter out = pageContext.getOut();
		try{
			out.println("</div>");
			out.println("<script>");
			out.println("	var url = String(location);");
			out.println("	var paginationPage_" + id + "  = \"paginationPage_" + id + "\"");
			out.println("	var paginationSize_" + id + " = \"paginationSize_" + id + "\"");
			out.println("	if (url.indexOf(paginationPage_" + id + ") > 0){ ");
			out.println("		var currentUrl = parseURL(url);");
			out.println("		var newPage = 1;");
			out.println("		var newPageSize = 10;");
			out.println("		for (var x in currentUrl.hash) {");
			out.println("			if (paginationPage_" + id + " == x) {");
			out.println("				newPage = currentUrl.hash[x] ;");
			out.println("				break;");
			out.println("			}");
			out.println("		}");
			out.println("		for (var x in currentUrl.hash) {");
			out.println("			if (paginationSize_" + id + " == x) {");
			out.println("				newPageSize = currentUrl.hash[x] ;");
			out.println("				break;");
			out.println("			}");
			out.println("		}");
			out.println("		$.ajax({");                                    
			out.println("			type: \"POST\",");
			out.println("			datatype:\"html\",");    
			out.println("			url: \"" + actionUrl + "\",");                                      
			out.println("			data: {pageSize:newPageSize, nextPage:newPage},");   
			out.println("			success: function(result){");
			out.println("				    $(\"#" + id +"\").html(result);");
			if (StringUtils.isNotEmpty(preActionUrl)) {
				out.println("				    //觸發預載");
				out.println("				  	preQueryByAJAX(\"" + preActionUrl + "\");");
			}
			out.println("			},");
			out.println("			error: function(data, status, e){");     
			out.println("				    alert(\"Ajax失敗 = \" + e.message);");                    
			out.println("				   }");
			out.println("			});");
			out.println("	}");
			out.println("</script>");
		}catch(Exception ex){
			throw new JspException(ex);
		}
		this.release();
		return EVAL_PAGE;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getPaginationType() {
		return paginationType;
	}

	public void setPaginationType(String paginationType) {
		this.paginationType = paginationType;
	}

	public String getReturnChkId() {
		return returnChkId;
	}

	public void setReturnChkId(String returnChkId) {
		this.returnChkId = returnChkId;
	}

	public Logger getLog() {
		return log;
	}

	public String getPreActionUrl() {
		return preActionUrl;
	}

	public void setPreActionUrl(String preActionUrl) {
		this.preActionUrl = preActionUrl;
	}

	public String getActionUrl() {
		return actionUrl;
	}

	public void setActionUrl(String actionUrl) {
		this.actionUrl = actionUrl;
	}

	

}
